<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use think\AjaxPage;
use think\Page;
use think\Db;
use think\Controller;
use phpDocumentor\Reflection\DocBlock\Tag\ExampleTag;
header("Content-Type: text/html; charset=utf8");
class Discuss extends MobileBase {
    public $user_id = 0;
    public $user = array();
    
    public function __construct(){
        parent::__construct();
        //进入讨论区需要登录
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
        else{
            header("location:" . U('Mobile/User/login'));
            exit();
        }
    }
    
    //人气讨论区
    public function index(){
        //获取所有酒厂
        $suppliers_list = M("suppliers")->where("is_check",1)->select();
        foreach ($suppliers_list as $key=> $value){
            //获取酒厂关注度
            $suppliers_list[$key]['attention'] = M("attention")->where(array('suppliers_id'=>$value['suppliers_id'],'status'=>1))->count();
            $attention[$key] = $suppliers_list[$key]['attention'];
            //获取4长酒厂销售最好的商品图
            $goods_list = M("goods")->where(array("suppliers_id"=>$value['suppliers_id'],'is_on_sale'=>1))->order("sales_sum desc")->limit(4)->select();
            foreach ($goods_list as $vv){
                $suppliers_list[$key]['goods_img'][] = $vv['original_img'];
            }
            //获取最新关注的5位用户头像
            $user_list = M("attention")
                            ->alias("a")
                            ->join("f_users u","a.user_id = u.user_id","left")
                            ->where("a.suppliers_id",$value['suppliers_id'])
                            ->order("a.addtime desc")
                            ->field("a.*,u.head_pic")
                            ->limit(5)
                            ->select(); 
            foreach ($user_list as $v){
                $suppliers_list[$key]['uesr_img'][] = empty($v['head_pic'])?"/public/upload/user/headPic/defaultheadpic.png":$v['head_pic'];
            }
        }
        
        //按关注度排序
        array_multisort($attention,SORT_DESC,SORT_NUMERIC,$suppliers_list);

        $this->assign("suppliers_list",$suppliers_list);
        return $this->fetch();
    }
    
    //讨论列表
    public function discuss_list() {

        $page = I("post.page",0);
        $limit = 4;
        $suppliers_id = I("id");
        //获取4条用户发帖
        $discuss = M("discuss")
        ->alias("d")
        ->join("f_users u","d.user_id = u.user_id","left")
        ->where(array("d.type"=>0,"d.suppliers_id"=>$suppliers_id))
        ->order("d.addtime desc")
        ->field("d.*,u.head_pic,u.nickname")
        ->limit($page*$limit,$limit)
        ->select();

        foreach ($discuss as $k=>$v){
//             //图像转换 
            $discuss[$k]['img'] = json_decode($v['img'],"true");
            //人物头像
            $discuss[$k]['head_pic'] = empty($discuss[$k]['head_pic'])?"/public/upload/user/headPic/defaultheadpic.png":$discuss[$k]['head_pic'];//系统logo
            //此贴的回复数
            $discuss[$k]['reply'] = M("discuss_reply")->where("discuss_id",$v['discuss_id'])->count();
            //时间格式转换
            $discuss[$k]['addtime'] = date("Y-m-d",$v['addtime']);
        }
        $discussCount = M("discuss")->where(array("type"=>0,"suppliers_id"=>$suppliers_id))->count();
        if(IS_AJAX){
            $back['status'] = 1;
            $back['msg'] = "帖子";
            $back['data'] = $discuss;
            echo  json_encode($back);
            exit();
        }

        //获取酒厂信息
        $suppliers_info = M("suppliers")
                          ->alias("a")
                          ->join('f_origin o',"a.origin_id = o.id","left")
                          ->where('a.suppliers_id',$suppliers_id)
                          ->find();
        //获取酒厂关注度
        $suppliers_info['attention'] =  M("attention")->where("suppliers_id",$suppliers_id)->count();

        //获取帖子列表
        //获取最近一条平台贴
        $systemDiscuss = M("discuss")->where(array("type"=>2,"suppliers_id"=>$suppliers_id))->order("addtime desc")->find();
        $systemDiscuss['logo_img'] = "/public/upload/user/headPic/defaultheadpic.png";//系统logo
        //获取两条酒厂贴
        $suppliersDiscuss = M("discuss")
                            ->alias("d")
                            ->join("f_suppliers s","d.suppliers_id = s.suppliers_id")
                            ->where(array("d.type"=>1,"d.suppliers_id"=>$suppliers_id))
                            ->order("d.addtime desc")
                            ->field("d.*,s.suppliers_logo")
                            ->limit(2)
                            ->select();
        foreach ($suppliersDiscuss as $key=>$value){
            $suppliersDiscuss[$key]['img'] = json_decode($value['img'],"true");
        }
        //查询用户是否关注
        $follow = M('attention')->where(array('suppliers_id'=>$suppliers_id,'user_id'=>$this->user_id))->find();
        if($follow){
            $attention = 1;
        }else{
            $attention = 0;
        }

        $this->assign("attention",$attention);
        $this->assign("suppliers_info",$suppliers_info);
        $this->assign("systemDiscuss",$systemDiscuss);
        $this->assign("suppliersDiscuss",$suppliersDiscuss);
        $this->assign("discuss",$discuss);
        $this->assign("pagecount",ceil($discussCount/$limit));
        return $this->fetch();
    }

    //添加讨论帖子
    public function add_discuss(){
        if(IS_AJAX){
            $data['title'] = I("post.title");
            $data['content'] = I("post.content");
            
            if($_POST['img']){
                $path = "./public/upload/discuss/";
                $result = save_img_base64($_POST['img'],$path,$this->user_id);
                if(is_array($result)){
                    $data['img'] = json_encode($result);
                } 
            }
            //将讨论帖入库
            $data['suppliers_id'] = I("post.suppliers_id");
            $data['user_id'] = $this->user_id;
            $data['addtime'] = time();
            $data['type'] = 0;
            $result = M("discuss")->add($data);
            if($result > 0){
                $back['status'] = 1;
                $back['msg'] = "操作成功";
                $back['data'] = $data;
            }else{
                $back['status'] = 0;
                $back['msg'] = "操作失败";
                $back['data'] = "数据添加失败";
            }
            echo json_encode($back);
            exit();
        }
    }
    
    //讨论详情
    public function discuss_info() {
        $discuss_id = I("get.id");
        //获取讨论详情
        $discuss_info = M("discuss")
                        ->alias("d")
                        ->join("f_users u","d.user_id = u.user_id","left")
                        ->join("f_suppliers s","s.suppliers_id = d.suppliers_id")
                        ->where("d.discuss_id",$discuss_id)
                        ->field("d.*,u.nickname,u.head_pic,s.suppliers_name,s.suppliers_logo")
                        ->find();
        if($discuss_info['type'] == 1){//酒厂贴
            $discuss_info['nickname'] = $discuss_info['suppliers_name'];
            $discuss_info['head_pic'] = ".".$discuss_info['suppliers_logo'];
        }elseif($discuss_info['type'] == 2){//平台贴
            $discuss_info['nickname'] = "封坛天下";
            $discuss_info['head_pic'] = "/public/upload/user/headPic/defaultheadpic.png";
        }
        $discuss_info['img'] = json_decode($discuss_info['img'],true);
        $discuss_info['head_pic'] = empty($discuss_info['head_pic'])?"/public/upload/user/headPic/defaultheadpic.png":$discuss_info['head_pic'];//系统logo
        //获取回复详情
        $discuss_reply_info = M("discuss_reply")
                                ->alias("dr")
                                ->join("f_users u","dr.user_id = u.user_id","left")
                                ->where(array("dr.discuss_id"=>$discuss_id,"dr.parent_id"=>0))
                                ->field("dr.*,u.nickname,u.head_pic")
                                ->select();
        //查询对回复的回复详情
        foreach ($discuss_reply_info as $key=>$value){
            $discuss_reply_info[$key]['reply_list'] = M("discuss_reply")
                                                    ->alias("dr")
                                                    ->join("f_users u","dr.user_id = u.user_id","left")
                                                    ->where(array("discuss_id"=>$discuss_id,"parent_id"=>$value['id']))
                                                    ->field("dr.*,u.nickname")
                                                    ->select();
            $discuss_reply_info[$key]['head_pic'] = empty($discuss_reply_info[$key]['head_pic'])?"/public/upload/user/headPic/defaultheadpic.png":$discuss_reply_info[$key]['head_pic'];//系统logo
        }
        $this->assign("discuss_info",$discuss_info);
        $this->assign("discuss_reply_info",$discuss_reply_info);
        return $this->fetch();
    }
    
    
    //添加回复
    public function  add_discuss_reply(){
        if(IS_AJAX){
            $data['content'] = I("post.content");
            $data['parent_id'] = I("post.parentid");
            $data['discuss_id'] = I("post.discuss_id");
            $data['user_id'] = $this->user_id;
            $data['addtime'] = time();
            $data['status'] = 1;
            $result = M("discuss_reply")->add($data);
            if($result > 0){
                $back['status'] = 1;
                $back['msg'] = "操作成功";
//                 if($data['parentid'] > 0){
//                     $back['data'] = 0;//对楼主的回复
//                 }else{
//                     $data['nickname'] = M('users')->where("user_id",$this->user_id)->field('nickname')->find();
//                     $data['addtime'] = date($data['addtime'],"m-d H:i");
//                     $back['data'] = $data;//对贴主的回复
//                 }
                $back['data'] = $result;
            }else{
                $back['status'] = 0;
                $back['msg'] = "操作失败";
                $back['data'] = "数据添加失败";
            }
            echo json_encode($back);
            exit();
        }
    }
}